package com.phiture.erp.produce.core.service.impl;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.phiture.erp.produce.core.pojo.vo.ErpProduceProcessPageReqVO;
import com.phiture.erp.produce.core.pojo.vo.ErpProduceProcessSaveReqVO;
import com.phiture.erp.produce.core.service.ErpProduceProcessService;
import com.phiture.erp.produce.dal.entity.ErpProduceProcessDO;
import com.phiture.erp.produce.dal.mapper.ErpProduceProcessMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import java.util.Collection;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static com.phiture.erp.common.constants.ErrorCodeConstants.PRODUCE_PROCESS_NOT_EXISTS;

/**
 * 工艺工序 Service 实现类
 *
 * @author lekaihuai
 */
@Service
@Validated
public class ErpProduceProcessServiceImpl implements ErpProduceProcessService {

    @Resource
    private ErpProduceProcessMapper produceProcessMapper;

    @Override
    public Long createProduceProcess(ErpProduceProcessSaveReqVO createReqVO) {
        // 插入
        ErpProduceProcessDO produceProcess = BeanUtils.toBean(createReqVO, ErpProduceProcessDO.class);
        produceProcessMapper.insert(produceProcess);
        // 返回
        return produceProcess.getId();
    }

    @Override
    public void updateProduceProcess(ErpProduceProcessSaveReqVO updateReqVO) {
        // 校验存在
        validateProduceProcessExists(updateReqVO.getId());
        // 更新
        ErpProduceProcessDO updateObj = BeanUtils.toBean(updateReqVO, ErpProduceProcessDO.class);
        produceProcessMapper.updateById(updateObj);
    }

    @Override
    public void deleteProduceProcess(Long id) {
        // 校验存在
        validateProduceProcessExists(id);
        // 删除
        produceProcessMapper.deleteById(id);
    }

    private void validateProduceProcessExists(Long id) {
        if (produceProcessMapper.selectById(id) == null) {
            throw exception(PRODUCE_PROCESS_NOT_EXISTS);
        }
    }

    @Override
    public ErpProduceProcessDO getProduceProcess(Long id) {
        return produceProcessMapper.selectById(id);
    }

    @Override
    public PageResult<ErpProduceProcessDO> getProduceProcessPage(ErpProduceProcessPageReqVO reqVO) {
            return produceProcessMapper.selectPage(reqVO, new LambdaQueryWrapperX<ErpProduceProcessDO>()
                    .eqIfPresent(ErpProduceProcessDO::getCodeNo, reqVO.getCodeNo())
                    .likeIfPresent(ErpProduceProcessDO::getProcessName, reqVO.getProcessName())
                    .eqIfPresent(ErpProduceProcessDO::getStatus, reqVO.getStatus())
                    .orderByDesc(ErpProduceProcessDO::getId));
        }

    @Override
    public List<ErpProduceProcessDO> getProduceProcessAll() {
        return produceProcessMapper.selectList();
    }

    @Override
    public Map<Long, ErpProduceProcessDO> getProcessMap(Collection<Long> ids) {
        List<ErpProduceProcessDO> list = produceProcessMapper.selectBatchIds(ids);
        return convertMap(list, ErpProduceProcessDO::getId);
    }
}